package com.orm.analysis.service;

import com.orm.analysis.dao.ContactInfoDAO;
import com.orm.analysis.dao.OrderDAO;
import com.orm.analysis.dao.SalesmanDAO;
import com.orm.analysis.model.*;
import com.orm.analysis.util.GlobalConst;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: saman
 * Date: 10/12/13
 * Time: 7:29 AM
 * To change this template use File | Settings | File Templates.
 */
public class SalesmanService     implements GenericService<Salesman> {


    private static Logger log = Logger.getLogger(SalesmanService.class);

    private String salesmanDataFielDir;

    private SalesmanDAO dao;

    private ContactInfoDAO contactInfoDAO;

    private OrderDAO orderDAO;


    public OrderDAO getOrderDAO() {
        return orderDAO;
    }

    public void setOrderDAO(OrderDAO orderDAO) {
        this.orderDAO = orderDAO;
    }

    public ContactInfoDAO getContactInfoDAO() {
        return contactInfoDAO;
    }

    public void setContactInfoDAO(ContactInfoDAO contactInfoDAO) {
        this.contactInfoDAO = contactInfoDAO;
    }

    public SalesmanDAO getDao() {
        return dao;
    }

    public void setDao(SalesmanDAO dao) {
        this.dao = dao;
    }

    public String getSalesmanDataFielDir() {
        return salesmanDataFielDir;
    }

    public void setSalesmanDataFielDir(String salesmanDataFielDir) {
        this.salesmanDataFielDir = salesmanDataFielDir;
    }

    public Salesman create(Salesman entity) {
        dao.save(entity);
         return entity;
     }



    public void delete(Object id) {

        dao.delete(id);
    }


    public Salesman update(Salesman entity) {
        dao.update(entity);
          return entity;
      }

    public List<Salesman> list(int page, int size) {
        return dao.list(page,size);
          }


    public Salesman getById(Salesman entity) {
        return dao.getById(entity.getId());
    }


    public Salesman getById(Integer id) {
        return dao.getById(id);
            }


    public void executeImproter() throws ParseException, IOException {


                try{
                log.info("File  >>" + salesmanDataFielDir);
                BufferedReader br = null;

                br = new BufferedReader(new FileReader(salesmanDataFielDir));

                StringBuilder sb = new StringBuilder();
                String line = br.readLine();
                line = br.readLine();//Skip 1 line

                while (line != null) {
                    log.info("File  >>" + line);
                    //ID( not need) FIRST_NAME,LAST_NAME,REGISTRATION_DATE,contactInfo, SALESMAN_CODE,COMMISSION_RATE,COMMISSION_AMOUNT,custOrders~custOrders

                    Salesman salesman = new Salesman();
                    String[] data = line.split("[,]");



                    salesman.setFirstName(data[0]);
                    salesman.setLastName(data[1]);

                    salesman.setRegistrationDate(GlobalConst.simpleDateFormat.parse(data[2].trim()));

                    if (data[3].trim().length() > 0) {
                        // salesman.setContactInfo(contactInfoDAO.getById(Integer.valueOf((data[3].trim()))));
                        salesman.setContactInfo(new ContactInfo(Integer.valueOf((data[3].trim()))));
                    }


                    salesman.setSalesmanCode(data[4]);
                    salesman.setCommissionRete(Double.valueOf(data[5]));
                    salesman.setCommissionAmount(Double.valueOf(data[6]));

                    dao.save(salesman);

                    if (data.length > 7 && data[7] != null && data[7].length() > 0) {
                        List<CustOrder> custOrderList = new ArrayList<CustOrder>();
                        String[] custOrderIdArray = data[7].split("[~]");
                        for (int i = 0; i < custOrderIdArray.length; i++) {
//                            custOrderList.add(orderDAO.getById(Integer.valueOf(custOrderIdArray[i])));
                            custOrderList.add(new CustOrder(Integer.valueOf(custOrderIdArray[i])));
                        }
                        if (custOrderIdArray.length > 0) {
                            salesman.setCustOrders(custOrderList);
                        }
                    }


                    dao.save(salesman);

                    line = br.readLine();


                }

                br.close();

                }catch(Exception e ){
                    e.printStackTrace();
                }

            }
}
